package com.zzyl.serve.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.common.utils.IDCardUtils;
import com.zzyl.common.utils.SecurityUtils;

import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.constant.UserConstants;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.framework.interceptor.MyMetaObjectHandler;
import com.zzyl.serve.domain.NursingTaskQuery;
import com.zzyl.serve.vo.NursingTaskDetailVo;
import com.zzyl.serve.dto.NursingTaskDto;
import com.zzyl.serve.vo.NursingTaskVo;
import com.zzyl.serve.vo.NursingTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingTaskMapper;
import com.zzyl.serve.domain.NursingTask;
import com.zzyl.serve.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService
{
    @Autowired
    private NursingTaskMapper nursingTaskMapper;

    @Autowired
    private MyMetaObjectHandler myMetaObjectHandler;

    /**
     * 查询护理任务
     *
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTaskDetailVo selectNursingTaskById(Long id)
    {
        NursingTaskDetailVo vo = nursingTaskMapper.selectTaskDetailById(id);
        Long userId = SecurityUtils.getUserId();
        String updater = nursingTaskMapper.selectNameByUserId(userId.toString());
        vo.setUpdater(updater);
        if (vo.getSex().equals("1")) {
            vo.setSex("男");
        } else {
            vo.setSex("女");
        }
        int age = IDCardUtils.getAgeByIdCard(vo.getIdCard());
        vo.setAge(age);




        List<String> ids = Arrays.asList(vo.getNursingId().split(","));
        List<String> names = new ArrayList<>();
        for (String nurseId : ids) {
            String name = nursingTaskMapper.selectNameByUserId(nurseId);
            names.add(name);
        }
        vo.setNursingName(names);
        vo.setUpdateTime(LocalDateTime.now());
        return vo;




    }

    /**
     * 查询护理任务列表
     *
     * @param query
     * @return 护理任务
     */
    @Override
    public List<NursingTaskVo> selectNursingTaskList(NursingTaskQuery query)
    {

        List<NursingTask> list = nursingTaskMapper.selectNursingTaskList(query);
        List<NursingTaskVo> collect = list.stream().map(task -> {
            NursingTaskVo vo = BeanUtil.toBean(task, NursingTaskVo.class);

            String[] nursingId;
            if (task.getNursingId().contains("[")){
                nursingId= task.getNursingId().substring(1, task.getNursingId().length() - 1).split( ",");
            }else {
                nursingId = task.getNursingId().split( ",");
            }
            List<String> ids = Arrays.asList(nursingId);
            List<String> names = new ArrayList<>();
            for (String userId : ids) {
                String name = nursingTaskMapper.selectNameByUserId(userId);
                names.add(name);
            }
            vo.setNursingName(names);
            return vo;
        }).collect(Collectors.toList());


        return collect;

    }

    /**
     * 新增护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask)
    {
        return save(nursingTask) ? 1 : 0;
    }

    /**
     * 修改护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask)
    {
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 批量删除护理任务
     * 
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理任务信息
     * 
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 取消护理任务
     * @param dto
     * @return
     */
    @Override
    public int cancelNursingTask(NursingTaskDto dto) {
        NursingTask nursingTask = new NursingTask();
        nursingTask.setId(dto.getTaskId());
        nursingTask.setCancelReason(dto.getReason());

        // 设置状态为已取消 3
        nursingTask.setStatus(Long.valueOf(3));
        //获取当前登录用户的id
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        nursingTask.setUpdateBy(userId+"");
        boolean i = updateById(nursingTask);
        return i ? 1 : 0;
    }

    /**
     * 任务执行
     */
    @Override
    public int doTask(NursingTaskDto dto) {
        NursingTask nursingTask = new NursingTask();
        BeanUtil.copyProperties(dto,nursingTask);
        nursingTask.setId(dto.getTaskId());
        Long userId = SecurityUtils.getUserId();
        nursingTask.setUpdateBy(String.valueOf(userId));
        nursingTask.setRealServerTime(LocalDateTime.now());
        nursingTask.setStatus(Long.valueOf(2));
        boolean result = updateById(nursingTask);
        return result ? 1 : 0;
    }

    /**
     * 修改任务时间
     * @param dto
     */
    @Override
    public void updateTime(NursingTaskDto dto) {
        NursingTask nursingTask = new NursingTask();
        BeanUtil.copyProperties(dto,nursingTask);
        nursingTask.setId(dto.getTaskId());
        nursingTask.setUpdateBy(SecurityUtils.getUserId()+"");

        boolean b = updateById(nursingTask);


    }
}
